package com.example.macbook.zarafa;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.com.zarafa.model.User;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;

public class LoginActivity extends AppCompatActivity {
    private EditText editText_zhanghao;
    private EditText editText_code;
    private Button button_login;
    private Button button_loginreg;

    private String zhanghao;
    private String mima;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        editText_zhanghao= (EditText) findViewById(R.id.phone);
        editText_code= (EditText) findViewById(R.id.code);

        button_login= (Button) findViewById(R.id.login);
        button_loginreg= (Button) findViewById(R.id.login_regist);
        /**
         * 登录
         */
        button_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();

            }
        });
        /**
         * 注册
         */
        button_loginreg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setClass(LoginActivity.this,RegistActivity.class);
                startActivity(intent);
            }
        });

    }


    public void login(){

        zhanghao=editText_zhanghao.getText().toString();
        mima=editText_code.getText().toString();

        BmobUser.loginByAccount(this, zhanghao, mima, new LogInListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (user != null) {
                 toast("登录成功");
                    Intent intent=new Intent();
                    intent.setClass(LoginActivity.this,MainActivity.class);
                    startActivity(intent);

                }else {
                    toast("账户名或密码错误");
                }


            }
        });
    }

    public void toast(String toast) {
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show();
    };


}
